package com.bocloud.cmp.web.controller.resource;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.web.controller.BasicController;
import com.bocloud.common.enums.BoCloudMethod;
import com.bocloud.common.enums.BoCloudService;
import com.bocloud.common.model.BsmResult;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.JSONTools;
import com.bocloud.common.utils.MapTools;
import com.bocloud.common.utils.ResultTools;
import com.bocloud.registry.http.core.ServiceFactory;
import com.bocloud.registry.http.model.RemoteService;
import com.bocloud.registry.utils.UrlTranslator;

/**
 * 云主机相关入口
 * 
 * @author DZG
 * @since V1.0 2016年9月12日
 */
@RestController
@RequestMapping("/vm")
public class CloudServerController {

    private static final String BASE_SERVICE = "/vm";
    private static final BoCloudService SERVICE = BoCloudService.Cmp;
    @Autowired
    private ServiceFactory serviceFactory;
    @Autowired
    private BasicController basicController;

    /**
     * 获取云主机列表
     * 
     * @param page
     *            当前页码
     * @param rows
     *            页面数据大小
     * @param params
     *            查询参数，例如：[{"param":{"name":"aaa","password":"1245"},"sign":
     *            "EQ|UEQ"},{"param":{"name":"aaa","password":"1245"},"sign":
     *            "EQ|UEQ"}]
     * @param sorter
     *            排序参数，例如：{"name":0|1,"password":0|1},0表示增序，1表示降序
     * @param simple
     *            简单查询标记，只有true和false,为false时返回云主机的详细信息，为true时只返回id和name值。
     * @return 数据结果集
     */
    @RequestMapping(value = "/list", method = { RequestMethod.POST })
    public BsmResult list(@RequestParam(value = Common.PAGE, required = false, defaultValue = Common.ONE) Integer page,
            @RequestParam(value = Common.ROWS, required = false, defaultValue = Common.TEN) Integer rows,
            @RequestParam(value = Common.PARAMS, required = false) String params,
            @RequestParam(value = Common.SORTER, required = false) String sorter,
            @RequestParam(value = Common.SIMPLE, required = false) boolean simple, HttpServletRequest request) {
        return basicController.list(page, rows, params, sorter, simple, BASE_SERVICE, request,
                CloudServerController.class.getSimpleName());
    }

    /**
     * 添加云主机
     * 
     * @param params
     *            云主机属性信息的json字符串，在数据转换时会发生异常，对外抛出400异常【Bad Request】
     * @return 添加结果
     */
    @RequestMapping(value = "/create", method = { RequestMethod.POST })
    public BsmResult create(@RequestParam(value = Common.PARAMS, required = true) String params,
            HttpServletRequest request) {
        return basicController.create(params, BASE_SERVICE, request, CloudServerController.class.getSimpleName());
    }

    /**
     * 克隆云主机
     * 
     * @param params
     *            云主机属性信息的json字符串，在数据转换时会发生异常，对外抛出400异常【Bad Request】
     * @return 添加结果
     */
    @RequestMapping(value = "/clone", method = { RequestMethod.POST })
    public BsmResult clone(@RequestParam(value = Common.PARAMS, required = true) String params,
            HttpServletRequest request) {
        JSONObject jsonObject = JSONTools.isJSONObj(params);
        if (null != jsonObject) {
            String url = BASE_SERVICE + "/clone";
            Map<String, Object> paramMap = MapTools.simpleMap(Common.PARAMS, params);
            RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.CREATE, null, paramMap,
                    request);
            return service.invoke();
        } else {
            return ResultTools.formatErrResult();
        }
    }

    /**
     * 迁移
     * 
     * @param params
     * @param request
     * @return
     */
    @RequestMapping(value = "/migrate", method = { RequestMethod.POST })
    public BsmResult migrate(@RequestParam(value = Common.PARAMS, required = true) String params,
            HttpServletRequest request) {
        JSONObject jsonObject = JSONTools.isJSONObj(params);
        if (null != jsonObject) {
            String url = BASE_SERVICE + "/migrate";
            Map<String, Object> paramMap = MapTools.simpleMap(Common.PARAMS, params);
            RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.CREATE, null, paramMap,
                    request);
            return service.invoke();
        } else {
            return ResultTools.formatErrResult();
        }
    }

    /**
     * 迁移(openstack)
     * 
     * @param params
     * @param request
     * @return
     */
    @RequestMapping(value = "/OSMigrate", method = { RequestMethod.POST })
    public BsmResult OSMigrate(@RequestParam(value = Common.PARAMS, required = true) String params,
            HttpServletRequest request) {
        JSONObject obj = JSONObject.parseObject(params);
        if (null != obj) {
            Map<String, Object> paramMap = MapTools.simpleMap(Common.PARAMS, params);
            RemoteService service = serviceFactory.safeBuild(SERVICE, BASE_SERVICE + "/OSMigrate", BoCloudMethod.OTHERS,
                    null, paramMap, request);
            return service.invoke();
        } else {
            return ResultTools.formatErrResult();
        }
    }

    /**
     * 创建快照
     * 
     * @param params
     * @param request
     * @return
     */
    @RequestMapping(value = "/snapshot", method = { RequestMethod.POST })
    public BsmResult snapShot(@RequestParam(value = Common.PARAMS, required = true) String params,
            HttpServletRequest request) {
        JSONObject jsonObject = JSONTools.isJSONObj(params);
        if (null != jsonObject) {
            String url = BASE_SERVICE + "/snapshot";
            Map<String, Object> paramMap = MapTools.simpleMap(Common.PARAMS, params);
            RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.CREATE, null, paramMap,
                    request);
            return service.invoke();
        } else {
            return ResultTools.formatErrResult();
        }
    }

    /**
     * 修改云主机
     * 
     * @param params
     *            云主机属性信息的json字符串，在数据转换时会发生异常
     * @return
     */
    @RequestMapping(value = "/modify", method = { RequestMethod.POST })
    public BsmResult modify(@RequestParam(value = Common.PARAMS, required = true) String params,
            HttpServletRequest request) {
        return basicController.modify(params, BASE_SERVICE, request, CloudServerController.class.getSimpleName());
    }

    /**
     * 删除云主机
     * 
     * @param params
     *            云主机属性信息的json字符串，在数据转换时会发生异常】
     * @return 操作结果
     */
    @RequestMapping(value = "/remove", method = { RequestMethod.POST })
    public BsmResult remove(@RequestParam(value = Common.PARAMS, required = true) String params,
            HttpServletRequest request) {
        return basicController.remove(params, BASE_SERVICE, request, CloudServerController.class.getSimpleName());
    }

    /**
     * 查看云主机详细信息
     * 
     * @param params
     *            云主机属性信息的json字符串，在数据转换时会发生异常，对外抛出400异常【Bad Request】
     * @return 操作结果
     */
    @RequestMapping(value = "/detail", method = { RequestMethod.GET })
    public BsmResult detail(@RequestParam(value = Common.PARAMS, required = true) String params,
            HttpServletRequest request) {
        return basicController.detail(params, BASE_SERVICE, request, CloudServerController.class.getSimpleName());
    }

    /**
     * 重启
     * 
     * @param params
     * @param request
     * @return
     */
    @RequestMapping(value = "/restart", method = { RequestMethod.POST })
    public BsmResult restart(@RequestParam(value = Common.PARAMS, required = true) String params,
            HttpServletRequest request) {
        JSONObject jsonObject = JSONTools.isJSONObj(params);
        if (null != jsonObject && jsonObject.containsKey(Common.ID)) {
            String url = UrlTranslator.translate(BASE_SERVICE + "/restart", jsonObject.get(Common.ID));
            RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.OPERATE, null, null, request);
            return service.invoke();
        } else {
            return ResultTools.formatErrResult();
        }
    }

    /**
     * 虚拟机批量重启
     * 
     * @param params
     * @param request
     * @return
     */
    @RequestMapping(value = "/batchReboot", method = { RequestMethod.POST })
    public BsmResult batchReboot(@RequestParam(value = Common.PARAMS, required = true) String params,
            HttpServletRequest request) {
        JSONObject jsonObject = JSONTools.isJSONObj(params);
        if (null != jsonObject) {
            String url = BASE_SERVICE + "/batchReboot";
            Map<String, Object> paramMap = MapTools.simpleMap(Common.PARAMS, jsonObject.get(Common.ID));
            RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.OPERATE, null, paramMap,
                    request);
            return service.invoke();
        } else {
            return ResultTools.formatErrResult();
        }
    }

    /**
     * 虚拟机批量启动
     * 
     * @param params
     * @param request
     * @return
     */
    @RequestMapping(value = "/batchStart", method = { RequestMethod.POST })
    public BsmResult batchStart(@RequestParam(value = Common.PARAMS, required = true) String params,
            HttpServletRequest request) {
        JSONObject jsonObject = JSONTools.isJSONObj(params);
        if (null != jsonObject) {
            String url = BASE_SERVICE + "/batchStart";
            Map<String, Object> paramMap = MapTools.simpleMap(Common.PARAMS, jsonObject.get(Common.ID));
            RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.OPERATE, null, paramMap,
                    request);
            return service.invoke();
        } else {
            return ResultTools.formatErrResult();
        }
    }

    /**
     * 虚拟机批量停止
     * 
     * @param params
     * @param request
     * @return
     */
    @RequestMapping(value = "/batchStop", method = { RequestMethod.POST })
    public BsmResult batchStop(@RequestParam(value = Common.PARAMS, required = true) String params,
            HttpServletRequest request) {
        JSONObject jsonObject = JSONTools.isJSONObj(params);
        if (null != jsonObject) {
            String url = BASE_SERVICE + "/batchStop";
            Map<String, Object> paramMap = MapTools.simpleMap(Common.PARAMS, jsonObject.get(Common.ID));
            RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.OPERATE, null, paramMap,
                    request);
            return service.invoke();
        } else {
            return ResultTools.formatErrResult();
        }
    }

    /**
     * 虚拟机批量删除
     * 
     * @param params
     * @param request
     * @return
     */
    @RequestMapping(value = "/batchRemove", method = { RequestMethod.POST })
    public BsmResult batchRemove(@RequestParam(value = Common.PARAMS, required = true) String params,
            HttpServletRequest request) {
        JSONObject jsonObject = JSONTools.isJSONObj(params);
        if (null != jsonObject) {
            String url = BASE_SERVICE + "/batchRemove";
            Map<String, Object> paramMap = MapTools.simpleMap(Common.PARAMS, jsonObject.get(Common.ID));
            RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.OPERATE, null, paramMap,
                    request);
            return service.invoke();
        } else {
            return ResultTools.formatErrResult();
        }
    }

    /**
     * 暂停
     * 
     * @param params
     * @param request
     * @return
     */
    @RequestMapping(value = "/pause", method = { RequestMethod.POST })
    public BsmResult pause(@RequestParam(value = Common.PARAMS, required = true) String params,
            HttpServletRequest request) {
        JSONObject jsonObject = JSONTools.isJSONObj(params);
        if (null != jsonObject && jsonObject.containsKey(Common.ID)) {
            String url = UrlTranslator.translate(BASE_SERVICE + "/pause", jsonObject.get(Common.ID));
            RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.OPERATE, null, null, request);
            return service.invoke();
        } else {
            return ResultTools.formatErrResult();
        }
    }

    /**
     * 恢复
     * 
     * @param params
     * @param request
     * @return
     */
    @RequestMapping(value = "/recovery", method = { RequestMethod.POST })
    public BsmResult recovery(@RequestParam(value = Common.PARAMS, required = true) String params,
            HttpServletRequest request) {
        JSONObject jsonObject = JSONTools.isJSONObj(params);
        if (null != jsonObject && jsonObject.containsKey(Common.ID)) {
            String url = UrlTranslator.translate(BASE_SERVICE + "/recovery", jsonObject.get(Common.ID));
            RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.OPERATE, null, null, request);
            return service.invoke();
        } else {
            return ResultTools.formatErrResult();
        }
    }

    /**
     * 挂起
     * 
     * @param params
     * @param request
     * @return
     */
    @RequestMapping(value = "/suspend", method = { RequestMethod.POST })
    public BsmResult suspend(@RequestParam(value = Common.PARAMS, required = true) String params,
            HttpServletRequest request) {
        JSONObject jsonObject = JSONTools.isJSONObj(params);
        if (null != jsonObject && jsonObject.containsKey(Common.ID)) {
            String url = UrlTranslator.translate(BASE_SERVICE + "/suspend", jsonObject.get(Common.ID));
            RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.OPERATE, null, null, request);
            return service.invoke();
        } else {
            return ResultTools.formatErrResult();
        }
    }

    /**
     * 激活
     * 
     * @param params
     * @param request
     * @return
     */
    @RequestMapping(value = "/active", method = { RequestMethod.POST })
    public BsmResult active(@RequestParam(value = Common.PARAMS, required = true) String params,
            HttpServletRequest request) {
        JSONObject jsonObject = JSONTools.isJSONObj(params);
        if (null != jsonObject && jsonObject.containsKey(Common.ID)) {
            String url = UrlTranslator.translate(BASE_SERVICE + "/active", jsonObject.get(Common.ID));
            RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.OPERATE, null, null, request);
            return service.invoke();
        } else {
            return ResultTools.formatErrResult();
        }
    }

    /**
     * 启动
     * 
     * @param params
     * @param request
     * @return
     */
    @RequestMapping(value = "/start", method = { RequestMethod.POST })
    public BsmResult start(@RequestParam(value = Common.PARAMS, required = true) String params,
            HttpServletRequest request) {
        JSONObject jsonObject = JSONTools.isJSONObj(params);
        if (null != jsonObject && jsonObject.containsKey(Common.ID)) {
            String url = UrlTranslator.translate(BASE_SERVICE + "/start", jsonObject.get(Common.ID));
            RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.OPERATE, null, null, request);
            return service.invoke();
        } else {
            return ResultTools.formatErrResult();
        }
    }

    /**
     * 关机
     * 
     * @param params
     * @param request
     * @return
     */
    @RequestMapping(value = "/stop", method = { RequestMethod.POST })
    public BsmResult stop(@RequestParam(value = Common.PARAMS, required = true) String params,
            HttpServletRequest request) {
        JSONObject jsonObject = JSONTools.isJSONObj(params);
        if (null != jsonObject && jsonObject.containsKey(Common.ID)) {
            String url = UrlTranslator.translate(BASE_SERVICE + "/stop", jsonObject.get(Common.ID));
            RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.OPERATE, null, null, request);
            return service.invoke();
        } else {
            return ResultTools.formatErrResult();
        }
    }

    /**
     * 同步
     * 
     * @param params
     * @param request
     * @return
     */
    @RequestMapping(value = "/sync", method = { RequestMethod.POST })
    public BsmResult sync(@RequestParam(value = Common.PARAMS, required = false) String params,
            HttpServletRequest request) {
        JSONObject obj = JSONObject.parseObject(params);
        if (null != obj) {
            Map<String, Object> paramMap = MapTools.simpleMap(Common.PARAMS, params);
            RemoteService service = serviceFactory.safeBuild(SERVICE, BASE_SERVICE + "/sync", BoCloudMethod.OTHERS,
                    null, paramMap, request);
            return service.invoke();
        } else {
            return ResultTools.formatErrResult();
        }
    }

    /**
     * 备份云主机
     */
    @RequestMapping(value = "/backup", method = { RequestMethod.POST })
    public BsmResult backup(@RequestParam(value = Common.PARAMS, required = true) String params,
            HttpServletRequest request) {
        JSONObject jsonObject = JSONTools.isJSONObj(params);
        if (null != jsonObject && jsonObject.containsKey(Common.ID)) {
            String url = UrlTranslator.translate(BASE_SERVICE + "/backup", jsonObject.get(Common.ID));
            RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.OPERATE, null, null, request);
            return service.invoke();
        } else {
            return ResultTools.formatErrResult();
        }
    }

    /**
     * 云主机快照
     */
    @RequestMapping(value = "/snapshoot", method = { RequestMethod.POST })
    public BsmResult snapshoot(@RequestParam(value = Common.PARAMS, required = true) String params,
            HttpServletRequest request) {
        JSONObject jsonObject = JSONTools.isJSONObj(params);
        if (null != jsonObject && jsonObject.containsKey(Common.ID)) {
            String url = UrlTranslator.translate(BASE_SERVICE + "/snapshoot", jsonObject.get(Common.ID));
            RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.OPERATE, null, null, request);
            return service.invoke();
        } else {
            return ResultTools.formatErrResult();
        }
    }

    /**
     * 获取监控数据
     * 
     * @param params
     * @param request
     * @return
     */
    @RequestMapping(value = "/performance", method = { RequestMethod.GET })
    public BsmResult performance(@RequestParam(value = Common.PARAMS, required = true) String params,
            HttpServletRequest request) {
        JSONObject obj = JSONObject.parseObject(params);
        if (null != obj) {
            Map<String, Object> paramMap = MapTools.simpleMap(Common.PARAMS, params);
            RemoteService service = serviceFactory.safeBuild(SERVICE, BASE_SERVICE + "/performance",
                    BoCloudMethod.SUBOBJ, null, paramMap, request);
            return service.invoke();
        } else {
            return ResultTools.formatErrResult();
        }
    }

    /**
     * 
     * vnc console
     * 
     * @param params
     * @param request
     * @return
     */
    @RequestMapping(value = "/vncconsole", method = { RequestMethod.POST })
    public BsmResult vncconsole(@RequestParam(value = Common.PARAMS, required = true) String params,
            HttpServletRequest request) {

        JSONObject obj = JSONObject.parseObject(params);
        if (null != obj) {
            Map<String, Object> paramMap = MapTools.simpleMap(Common.PARAMS, params);
            RemoteService service = serviceFactory.safeBuild(SERVICE, BASE_SERVICE + "/vncconsole",
                    BoCloudMethod.SUBOBJ, null, paramMap, request);
            return service.invoke();
        } else {
            return ResultTools.formatErrResult();
        }
    }

    /**
     * 网络列表
     * 
     * @param params
     * @param request
     * @return
     */
    @RequestMapping(value = "/networks", method = { RequestMethod.GET })
    public BsmResult networks(@RequestParam(value = Common.PARAMS, required = true) String params,
            HttpServletRequest request) {
        JSONObject jsonObject = JSONTools.isJSONObj(params);
        if (null != jsonObject && jsonObject.containsKey(Common.ID)) {
            String url = UrlTranslator.translate(BASE_SERVICE + "/networks", jsonObject.get(Common.ID));
            RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.BASIC, null, null, request);
            return service.invoke();
        } else {
            return ResultTools.formatErrResult();
        }
    }
    
    /**
     * 恢复
     * 
     * @param params
     * @param request
     * @return
     */
    @RequestMapping(value = "/unpause", method = { RequestMethod.POST })
    public BsmResult unpause(@RequestParam(value = Common.PARAMS, required = true) String params,
            HttpServletRequest request) {
        JSONObject jsonObject = JSONTools.isJSONObj(params);
        if (null != jsonObject && jsonObject.containsKey(Common.ID)) {
            String url = UrlTranslator.translate(BASE_SERVICE + "/unpause", jsonObject.get(Common.ID));
            RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.OPERATE, null, null, request);
            return service.invoke();
        } else {
            return ResultTools.formatErrResult();
        }
    }

}
